﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using DotNetCore.CAP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Application.IServices;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.IRepository;
using W.M.Materiel.Infrastruecure.Repository;

namespace W.M.Materiel.Application.Services
{
    internal class Product_configuration_attributeServices : BaseServices<product_configuration_attribute, string>, IProduct_configuration_attributeServices
    {
        private readonly IProduct_configuration_attributeRepository product_Configuration_AttributeRepository;
        private readonly ICapPublisher capPublisher;

        public Product_configuration_attributeServices(IProduct_configuration_attributeRepository product_Configuration_AttributeRepository, ICapPublisher capPublisher) : base(product_Configuration_AttributeRepository)
        {
            this.product_Configuration_AttributeRepository = product_Configuration_AttributeRepository;
            this.capPublisher = capPublisher;
        }

        /// <summary>
        /// 显示产品属性分页+查询
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件:产品属性名称</param>
        /// <returns>统一返回格式</returns>
        public async Task<PageETO<product_configuration_attribute>> Getproduct_configuration_attribute(int pageSize, int pageIndex, string? name)
        {
            PageETO<product_configuration_attribute> pageETO = new PageETO<product_configuration_attribute>();
            var list = await product_Configuration_AttributeRepository.GetShowInforAsync();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.product_attribute_name.Contains(name)).ToList();
            }
            pageETO.TotalCount = list.Count;
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount * 1.0 / pageSize);
            pageETO.Data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return pageETO;
        }

        /// <summary>
        /// 显示产品属性类别
        /// </summary>
        /// <returns></returns>
        public async Task<List<product_configuration_attribute>> Getproduct_configuration_attributes()
        {
            var list = await product_Configuration_AttributeRepository.GetShowInforAsync();
            return list;
        }

        /// <summary>
        /// 添加产品属性
        /// </summary>
        /// <param name="c">物料属性实体</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> GetProduct_configuration_attributeAdd(product_configuration_attribute c)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var list = await product_Configuration_AttributeRepository.AddInfor_Async(c);
            if (list > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "添加成功!";

                //添加成功需要传一个消息队列  传递哪个模块 还有编号
                string numberName = "物料系统-" + c.product_attribute_id;
                capPublisher.Publish("GetNewNumber", numberName);
                product_Configuration_AttributeRepository.CommitTranInfor();
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "添加失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 删除产品属性信息
        /// </summary>
        /// <param name="c">产品属性编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<int>> Getproduct_configuration_attributeDel(string? c)
        {
            ApiReturn<int> apiReturn = new ApiReturn<int>();
            var res = await product_Configuration_AttributeRepository.GetShowObjAsync(x => x.product_attribute_id == c);
            var list = await product_Configuration_AttributeRepository.DelInfor_Async(res);
            if (list != null)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 修改产品属性信息
        /// </summary>
        /// <param name="c">产品属性实体</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> Getproduct_configuration_attributeUpdate(product_configuration_attribute c)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var res = await product_Configuration_AttributeRepository.GetShowInforAsync(x => x.product_attribute_id.Equals(c.product_attribute_id));
            foreach (var item in res)
            {
                item.product_attribute_id = c.product_attribute_id;
                item.product_attribute_name = c.product_attribute_name;
                item.status = c.status;
                item.operating_time = c.operating_time;
                item.operators = c.operators;
                item.remark = c.remark;
            }
            var list = await product_Configuration_AttributeRepository.UpdInfor_Batch_Async(res);
            if (list > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "修改失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 反填产品属性信息
        /// </summary>
        /// <param name="material_attribute_number">产品属性编号</param>
        /// <returns>物料属性信息</returns>
        public async Task<product_configuration_attribute> Getbackfillproduct_configuration_attribute(string? product_attribute_id)
        {
            Expression<Func<product_configuration_attribute, bool>> wherename = x => x.product_attribute_id == product_attribute_id;
            var list = await product_Configuration_AttributeRepository.GetShowObjAsync(wherename);
            return list;
        }


    }
}
